package model.node;

import controller.Hypervisor;
import model.exceptions.DeathNode;
import model.messages.LocationClaim;
import model.messages.Message;
import model.messages.MessageInterface;
import model.options.Options;

public class LsmNode extends Node {

	/**
	 * Constructor require position of node and it's id	
	 * @param posX
	 * @param posY
	 */
	public LsmNode(Hypervisor h,float posX, float posY, int idOfClone){
		super(h,posX, posY,idOfClone);
	};
	
	/**
	 * If node, receives a location claim: with probability 1-p just ignores the message 
	 * (no other actions), else selects a pseudo random point in the unit-square area and call routingProcedure method.
	 * If receive a message (no location claim), it call routingProcedure.
	 * Always call checkForClone method
	 */
	protected void manageMessage(MessageInterface message) throws DeathNode{
		checkForClone(message);
		if (message instanceof LocationClaim){
			if(Math.random() <= Options.getInstance().getProbOfNeighbor()){
				for(int i=0; i < Options.getInstance().getNumDestination(); i++)
					routingProcedure(new Message((LocationClaim)message,(float)Math.random(),(float)Math.random()));
				hypervisor.decreaseMessages(1, this, message.getIdCreator()); //decrease the claim message received
			}
		}
		else{
			routingProcedure((Message)message);
		}
	}	
	
}
